package action.dropdown;

import java.util.List;

import model.dropdown.DCommon;
import model.dropdown.DUnitcat;
import service.dropdown.DropDownService;
import util.Sessions;
import core.action.BaseAction;
public class DropDownUpdateAction extends BaseAction{
	private DropDownService dropDownService;
	
	private DCommon cm;
	private int type;
	private int did;
	
	private List<?> dclist;
	private List<?> listid;
	private List<?> listname;
	private List<?> listpcode;
	private List<?> listtype;
	
	
	public List<?> getListid() {
		return listid;
	}
	public void setListid(List<?> listid) {
		this.listid = listid;
	}
	public List<?> getListname() {
		return listname;
	}
	public void setListname(List<?> listname) {
		this.listname = listname;
	}
	public List<?> getListpcode() {
		return listpcode;
	}
	public void setListpcode(List<?> listpcode) {
		this.listpcode = listpcode;
	}
	public List<?> getListtype() {
		return listtype;
	}
	public void setListtype(List<?> listtype) {
		this.listtype = listtype;
	}
	public int getDid() {
		return did;
	}
	public void setDid(int did) {
		this.did = did;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
		Sessions.set("dtype", type);
	}
	public DCommon getCm() {
		return cm;
	}
	public void setCm(DCommon cm) {
		this.cm = cm;
	}
	public List<?> getDclist() {
		return dclist;
	}
	public void setDclist(List<?> dclist) {
		this.dclist = dclist;
	}


	public DropDownService getDropDownService() {
		return dropDownService;
	}
	public void setDropDownService(DropDownService dropDownService) {
		this.dropDownService = dropDownService;
	}

	
	
	public String showCmList(){
		dclist = dropDownService.getCommonList(type, 0);
		super.successResultValue="cm";
		return "success";
	}
	
	public String updateCm(){
		//System.out.println(cm);
		setType(cm.getType());
		dropDownService.updateCommon(cm);
		dclist = dropDownService.getCommonList(cm.getType(), 0);
		super.successResultValue="cm";
		return "success";
	}
	
	
	public String updateAll(){
		if(listid != null){
			for(int i = 0;i < listid.size();i++){
				DCommon  tcm = new DCommon(Integer.parseInt((String)listid.get(i)),(String)listname.get(i),Integer.parseInt((String)listtype.get(i)),Integer.parseInt((String)listpcode.get(i)));
				dropDownService.updateCommon(tcm);
			}
			if(listtype.size() > 0){
				dclist = dropDownService.getCommonList(Integer.parseInt((String)listtype.get(0)), 0);
				setType(Integer.parseInt((String)listtype.get(0)));
			}
		}else{
			
			setType((Integer)Sessions.get("dtype"));
		
		}
		super.successResultValue="cm";
		return "success";
	}
	
	public String deleteCm(){
		System.out.println(type);
		System.out.println(did);
		cm = dropDownService.findById(did);
		dropDownService.deleteCommon(cm);
		dclist = dropDownService.getCommonList(type, 0);
		super.successResultValue="cm";
		return "success";
	}
	
	public String addCm(){
		/*if(dropDownService.exist(uc) == 1){
			MessageJump("代码已存在,可直接修改","BACK");
		}else if(dropDownService.exist(uc) == 2){
			MessageJump("名称已存在","BACK");
		}else{ 
			
		}*/
		setType(cm.getType());
		if(dropDownService.exist(cm) == 2){
			MessageJump("名称已存在","BACK");
		}
		
		cm.setPcode(0);
		updateCm();
		super.successResultValue="cm";
		return "success";
	}
	
	
}
